package com.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.CTicketMapper;
import com.mapper.CTicketRoundMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CTicket;
import com.pojo.entity.CTicketPlan;
import com.pojo.entity.CTicketRound;
import com.pojo.vo.CTicketPlanVo;
import com.pojo.vo.CTicketRoundVo;
import com.pojo.vo.CTicketVo;
import com.service.CTicketPlanService;
import com.service.CTicketRoundService;
import com.service.CTicketService;
import com.util.PojoUtil;
import com.util.RandomNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Service("CTicketRoundService")
public class CTicketRoundServiceImpl implements CTicketRoundService{
    @Autowired
    CTicketRoundMapper cTicketRoundMapper;
    @Autowired
    CTicketService cTicketService;
    @Autowired
    CTicketPlanService cTicketPlanService;
    @Override
    public Message<List<CTicketRoundVo>> list(CTicketRoundVo cTicketRoundVo) {
        QueryWrapper<CTicketRound> queryWrapper = new QueryWrapper<>();
        List<CTicketRound> cTicketRounds = cTicketRoundMapper.selectList(queryWrapper);
        List<CTicketRoundVo>  cTicketRoundVos = new ArrayList<>();
        for (int i = 0; i < cTicketRounds.size(); i++) {
            CTicketRound cTicketRound = cTicketRounds.get(i);
            cTicketRoundVos.add(PojoUtil.copyObject(cTicketRound,CTicketRoundVo.class));
        }
        return Message.successMessage(1L,cTicketRoundVos);
    }

    @Override
    public Message<CTicketRoundVo> selectOne(Long planId) {
        QueryWrapper<CTicketRound> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id",planId);
        CTicketRound cTicketRound = cTicketRoundMapper.selectOne(queryWrapper);
        if (cTicketRound != null)return Message.successMessage(1L,PojoUtil.copyObject(cTicketRound,CTicketRoundVo.class));
        else return Message.successMessage(1L,null);
    }

    @Override
    public Message<CTicketRoundVo> add(CTicketRoundVo cTicketRoundVo) {
        int i = cTicketRoundMapper.insert(PojoUtil.copyObject(cTicketRoundVo, CTicketRound.class));
        if (i == 0)return Message.errorMessage("1001","插入失败");
        return Message.successMessage(1L,cTicketRoundVo);
    }

    @Override
    public Message<CTicketRoundVo> update(CTicketRoundVo cTicketRoundVo) {
        int i = cTicketRoundMapper.updateById(PojoUtil.copyObject(cTicketRoundVo, CTicketRound.class));
        if (i == 0)return Message.errorMessage("1001","修改失败");
        return Message.successMessage(1L,cTicketRoundVo);
    }

    @Override
    public Message<CTicketRoundVo> delete(Long planId) {
        QueryWrapper<CTicketRound> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id",planId);
        CTicketPlanVo cTicketPlanVo = new CTicketPlanVo();
        cTicketPlanVo.setPlanId(planId);
        List<CTicketPlanVo> data = cTicketPlanService.list(cTicketPlanVo).getData();
        for (int i = 0; i < data.size(); i++) {
            Long id = data.get(i).getId();
            cTicketPlanService.delete(id);
        }
        int i = cTicketRoundMapper.delete(queryWrapper);
        if (i == 0)return Message.errorMessage("1001","删除失败");
        return Message.successMessage(1L,null);
    }

    @Override
    public Message<List<CTicketVo>> designTicket(CTicketRoundVo cTicketRoundVo) {
        CTicketVo cTicketVo = new CTicketVo();
        cTicketVo.setPlanId(cTicketRoundVo.getPlanId());
        cTicketVo.setState(1L);//查询所有未抽号的
        List<CTicketVo> data = cTicketService.list(cTicketVo).getData();
        //根据付款时间排序
        List<CTicketVo> sortList =
                data.stream().sorted(Comparator.comparing(CTicketVo::getChargeTime)).collect(Collectors.toList());
        //根据场次号查询出场次的允许参与人数user_count
        CTicketPlanVo cTicketPlanVo1 = new CTicketPlanVo();
        cTicketPlanVo1.setPlanId(cTicketVo.getPlanId());
        CTicketPlanVo cTicketPlanVo = cTicketPlanService.selectOne(cTicketPlanVo1).getData();
        Long userCount = cTicketPlanVo.getUserCount();
        int[] ints = RandomNumUtil.randomArray(1, Math.toIntExact(userCount), data.size());
        for (int i = 0; i < ints.length; i++) {
            CTicketVo cTicketVo1 = sortList.get(i);
            cTicketVo1.setState(2L);
            cTicketVo1.setSite((long) ints[i]);
            cTicketService.update(cTicketVo1);
        }
         return cTicketService.list(cTicketVo);
    }
}
